package com.sydx.utils;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
// 全局异常处理类
@ControllerAdvice
public class GlobalControllerAdvice {

    // error.jsp--全局变量
    private final String ERROR = "error";

    // 如果没有权限登录，直接跳转登录
    @ExceptionHandler(value=PermisissionException.class)
    public ModelAndView noPermission(PermisissionException e){
        ModelAndView modelAndView = new ModelAndView(ERROR);
        modelAndView.addObject(ERROR,e.getMessage());
        return modelAndView;
    }

    // 普通异常，返回json
    @ExceptionHandler(value=RuntimeException.class)
    @ResponseBody
    public Map<String,Object> runtimeException(RuntimeException e){
        e.printStackTrace();
        return MapControl.getInstance().error().getMap();
    }


}
